package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	
	public class FrameRegister extends JFrame
	{
		//members define
		private var _labelMessage:JLabel;
		private var _txtName:JTextField;
		private var _txtPassword:JTextField;
		private var _txtPasswordComfirm:JTextField;
		private var panel78:JPanel;
		private var _btn_register:JButton;
		private var _btn_cancel:JButton;
		
		public function FrameRegister()
		{
			super();
			setClosable(false);
			setResizable(false);  
			setTitleBar(null);
			setBorder(new EmptyBorder());
			this.setBackgroundDecorator(new SolidBackground(new ASColor(0,0)));
			
			//component creation
			setSize(new IntDimension(240, 163));
			setPreferredSize(new IntDimension(240, 163));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			_labelMessage = new JLabel();
			_labelMessage.setLocation(new IntPoint(0, 102));
			_labelMessage.setSize(new IntDimension(240, 21));
			_labelMessage.setPreferredSize(new IntDimension(240, 21));
			_labelMessage.setText("label");
			
			_txtName = new JTextField();
			_txtName.setLocation(new IntPoint(70, 25));
			_txtName.setSize(new IntDimension(137, 21));
			_txtName.setPreferredSize(new IntDimension(137, 21));
			
			_txtPassword = new JTextField();
			_txtPassword.setLocation(new IntPoint(70, 53));
			_txtPassword.setSize(new IntDimension(137, 21));
			_txtPassword.setPreferredSize(new IntDimension(137, 21));
			
			_txtPasswordComfirm = new JTextField();
			_txtPasswordComfirm.setLocation(new IntPoint(70, 81));
			_txtPasswordComfirm.setSize(new IntDimension(137, 21));
			_txtPasswordComfirm.setPreferredSize(new IntDimension(137, 21));
			
			panel78 = new JPanel();
			panel78.setLocation(new IntPoint(0, 125));
			panel78.setSize(new IntDimension(240, 25));
			panel78.setPreferredSize(new IntDimension(240, 25));
			var layout1:FlowLayout = new FlowLayout();
			layout1.setAlignment(AsWingConstants.CENTER);
			layout1.setHgap(20);
			layout1.setVgap(0);
			panel78.setLayout(layout1);
			
			_btn_register = new JButton();
			_btn_register.setLocation(new IntPoint(5, 5));
			_btn_register.setSize(new IntDimension(57, 25));
			_btn_register.setPreferredSize(new IntDimension(57, 25));
			_btn_register.setText("label");
			
			_btn_cancel = new JButton();
			_btn_cancel.setLocation(new IntPoint(41, 5));
			_btn_cancel.setSize(new IntDimension(57, 25));
			_btn_cancel.setPreferredSize(new IntDimension(57, 25));
			_btn_cancel.setText("label");
			
			//component layoution
			getContentPane().append(_labelMessage);
			getContentPane().append(_txtName);
			getContentPane().append(_txtPassword);
			getContentPane().append(_txtPasswordComfirm);
			getContentPane().append(panel78);
			
			panel78.append(_btn_register);
			panel78.append(_btn_cancel);
			
			// 增加循环背景控制
			_txtName.setBackgroundDecorator(null);
			_txtName.setMaxChars(16);
			_txtName.setRestrict('0-9,a-z,A-Z,_');
			
			//_txtName.setHorizontalAlignment
			
			_txtPassword.setBackgroundDecorator(null);
			_txtPassword.setDisplayAsPassword(true);
			_txtPassword.setMaxChars(16);
			
			_txtPasswordComfirm.setBackgroundDecorator(null);
			_txtPasswordComfirm.setDisplayAsPassword(true);
			_txtPasswordComfirm.setMaxChars(16);
			
			var txtFormat:TextFormat = new TextFormat("Courier New", 14, 0xFFFFFF, false, false, false);
			
			_txtName.setDefaultTextFormat(txtFormat);
			_txtPassword.setDefaultTextFormat(txtFormat);
			_txtPasswordComfirm.setDefaultTextFormat(txtFormat);
			
			_txtName.addActionListener(onRegister);
			_txtPassword.addActionListener(onRegister);
			_txtPasswordComfirm.addActionListener(onRegister);
			
			var filter:GlowFilter;
			filter = new GlowFilter(0x000000, 1, 2, 2, 100);
			_txtName.filters = new Array(filter);
			_txtPassword.filters = new Array(filter);
			_txtPasswordComfirm.filters = new Array(filter);
			
			_labelMessage.setFont(new ASFont("宋体", 14, false, false, false, false));
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			_labelMessage.setTextFilters(new Array(filter));
			_labelMessage.setForeground( ASColor.RED ); 
			_labelMessage.setText("");
			
			//_btn_register = new JButton();//'注册'
			//_btn_cancel = new JButton();//'取消'
			
			_btn_register.setText("");
			_btn_cancel.setText("");
			
			_btn_register.addActionListener(onRegister);
			_btn_cancel.addActionListener(onCancel);
			
			_btn_register.wrapSimpleButton(Client.getUIElement("btn_register") as SimpleButton);
			_btn_cancel.wrapSimpleButton(Client.getUIElement("btn_cancel") as SimpleButton);
			
			var img_login:DisplayObject = Client.getUIElement("img_register") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_login));
			
			setSizeWH(img_login.width,img_login.height);
			setLocationXY((Engine.W - img_login.width) / 2,(Engine.H - img_login.height)/2);
		}
		
		protected function onRegister(e:Event):void
		{
			//closeReleased();
			//JOptionPane.showMessageDialog("注册","注册");
			//_panelMain.openMap();
			if(_txtName.getText().length <= 0)
			{
				_labelMessage.setText("请输入用户名!");
				validate();
				return;
			}
			if(_txtPassword.getText().length <= 0)
			{
				_labelMessage.setText("请输入密码!");
				validate();
				return;
			}
			if(_txtPassword.getText() != _txtPasswordComfirm.getText())
			{
				_labelMessage.setText("两次输入的密码不相同!");
				validate();
				return;
			}
			
			//Client.getInstance().sendPacketLogic(3,[_txtName.getText(),_txtPassword.getText()]);
			//Client.getInstance().addEventListener("3", phRegister_3);
			//_panelMain.createCharacter();
			//closeReleased();
			_txtName.setEnabled(false);
			_txtPassword.setEnabled(false);
			_txtPasswordComfirm.setEnabled(false);
			_btn_register.setEnabled(false);
			_btn_cancel.setEnabled(false);
			
			if(!Client.getInstance().beConnected)
			{
				if(!Client.getInstance().beConnecting)
					Client.getInstance().connect();
				showMessage("正在连接服务器...");
				Client.getInstance().socket.addEventListener(Event.CONNECT, handleSocketConnect);
			}
			else
			{
				showMessage("正在注册...")
				Client.getInstance().sendPacketLogic(3,[Engine.account_type,_txtName.getText(),_txtPassword.getText()]);
				Client.getInstance().addEventListener("3", phRegister_3);
			}
		}
		
		private function handleSocketConnect(e:Event):void
		{
			Client.getInstance().socket.removeEventListener(Event.CONNECT, handleSocketConnect);
			showMessage("连接服务器成功,正在注册...")
			Client.getInstance().sendPacketLogic(3,[Engine.account_type,_txtName.getText(),_txtPassword.getText()]);
			Client.getInstance().addEventListener("3", phRegister_3);
		}
		
		private function phRegister_3(e:PacketEvent):void
		{
			Client.getInstance().removeEventListener("3", phRegister_3);
			
			_txtName.setEnabled(true);
			_txtPassword.setEnabled(true);
			_txtPasswordComfirm.setEnabled(true);
			_btn_register.setEnabled(true);
			_btn_cancel.setEnabled(true);
			
			var msg:Array = e.data as Array;
			trace("phRegister_3", msg);
			switch(msg[0])
			{
				case 0:
				{
					showMessage("注册成功!");
					FrameCreate.show();
					closeReleased();
					CharInfo.accountId = msg[1];
					break;
				}
				case 1:
				{
					showMessage("帐号名已经被使用!");
					break;
				}
				case 2:
				{
					showMessage("数据库操作出错!");
					break;
				}
			}
		}
		
		private function showMessage(msg:String):void
		{
			_labelMessage.setText(msg);
			validate();
		}
		
		protected function onCancel(e:Event):void
		{
			FrameLogin.show();
			closeReleased();;
		}
		
		public static var _frame:FrameRegister = null;
		public static function show():void
		{
			FrameLogin.hide();
			FrameCreate.hide();
			if(isShowing())
			{
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameRegister();
				PanelMain.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMain.getInstance().setBackgroundDecorator(null);
			PanelMain.getInstance().setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_create_background") as DisplayObject));
			PanelMain.getInstance().showPanelMainHelp(false);
			PanelMain.getInstance().bringToTop(_frame);
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}